﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.IBUS;
using SD.IDAL;
using SD.DAL;
using SD.Entities;
using System.Data.SqlClient;
using System.Data;

namespace SD.BUS
{
    public class TeamBIZ : BaseBIZ,ITeamBIZ
    {
        /// <summary>
        /// Create TeamDAO class
        /// </summary>
        ITeamDAO teamDAO = new TeamDAO();
        /// <summary>
        /// Get List Team by Filter
        /// </summary>
        /// <param name="filterExpression"></param>
        /// <param name="includeInactive"></param>
        /// <param name="departID"></param>
        /// <returns></returns>
        public DataTable GetByFilter(string filterExpression, bool includeInactive, string departID)
        {
            DataTable dtbResult = new DataTable();
            //Create DataColumn
            dtbResult.Columns.Add("TeamID");
            dtbResult.Columns.Add("Name");
            dtbResult.Columns.Add("Line1");
            dtbResult.Columns.Add("Postcode");
            dtbResult.Columns.Add("Surname");
            dtbResult.Columns.Add("IsActive");

            try
            {
                List<Team> lstTeam = teamDAO.GetByFilter(filterExpression, includeInactive, departID);
                if (lstTeam.Count > 0)
                {
                    foreach (Team team in lstTeam)
                    {
                        dtbResult.Rows.Add(team.TeamId, team.Name,
                                            team.Address != null ? team.Address.Line1 : "N/A",
                                            team.Address != null ? team.Address.PostCode : "N/A",
                                            team.Contact != null ? team.Contact.FirstName : "N/A",
                                            team.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }
        /// <summary>
        /// Get All List Team
        /// </summary>
        /// <param name="includeInactive"></param>
        /// <param name="teamID"></param>
        /// <returns></returns>
        public DataTable GetAll(bool includeInactive, string departID)
        {
            DataTable dtbResult = new DataTable();
            dtbResult.Columns.Add("TeamID");
            dtbResult.Columns.Add("Name");
            dtbResult.Columns.Add("Line1");
            dtbResult.Columns.Add("Postcode");
            dtbResult.Columns.Add("Surname");
            dtbResult.Columns.Add("IsActive");

            try
            {
                List<Team> lstTeam = teamDAO.GetAll(includeInactive, departID);
                if (lstTeam.Count > 0)
                {
                    foreach (Team team in lstTeam)
                    {
                        dtbResult.Rows.Add(team.TeamId, team.Name,
                                            team.Address != null ? team.Address.Line1 : "N/A",
                                            team.Address != null ? team.Address.PostCode : "N/A",
                                            team.Contact != null ? team.Contact.FirstName : "N/A",
                                            team.IsActive);
                    }
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return dtbResult;
        }
        /// <summary>
        /// Get Team by Name
        /// </summary>
        /// <param name="dirName"></param>
        /// <returns></returns>
        public List<Team> GetDirByName(string dirName)
        {
            List<Team> result = new List<Team>();

            try
            {
                result = teamDAO.GetDirByName(dirName);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return result;
        }
        /// <summary>
        /// Get a Team by ID
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public Team GetById(string teamId)
        {
            Team result = null;

            try
            {
                result = teamDAO.GetById(teamId);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

            return result;
        }
        /// <summary>
        /// Insert a Team
        /// </summary>
        /// <param name="team"></param>
        /// <returns></returns>
        public void Insert(Team team)
        {
            try
            {
                team.TeamId = teamDAO.Insert(team);

            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Update a Team
        /// </summary>
        /// <param name="team"></param>
        /// <returns></returns>
        public void Update(Team team)
        {
            try
            {
                teamDAO.Update(team);
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Set Active a Team
        /// </summary>
        /// <param name="teamId"></param>
        /// <param name="status"></param>
        public void SetActive(string teamId, bool status)
        {
            try
            {
                teamDAO.SetActive(teamId, status);
            }
            catch (SqlException ex)
            {
                throw ex;
            }

        }

    }
}
